package com.bugsnag.ohos;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

@SuppressWarnings("ConstantConditions")
public class Breadcrumb implements JsonStream.Streamable {
    final BreadcrumbInternal impl;
    private final Logger logger;

    /**
     * constructor
     *
     * @param message
     * @param logger
     */
    Breadcrumb(String message, Logger logger) {
        this.impl = new BreadcrumbInternal(message);
        this.logger = logger;
    }

    /**
     * constructor
     *
     * @param message
     * @param type
     * @param metadata
     * @param timestamp
     * @param logger
     */
    public Breadcrumb(String message,
                      BreadcrumbType type,
                      Map<String, Object> metadata,
                      Date timestamp,
                      Logger logger) {
        this.impl = new BreadcrumbInternal(message, type, metadata, timestamp);
        this.logger = logger;
    }

    private void logNull(String property) {
        logger.e("Invalid null value supplied to breadcrumb." + property + ", ignoring");
    }

    /**
     * Sets the description of the breadcrumb
     *
     * @param message
     */
    public void setMessage(String message) {
        if (message != null) {
            impl.message = message;
        } else {
            logNull("message");
        }
    }

    /**
     * Gets the description of the breadcrumb
     *
     * @return message
     */

    public String getMessage() {
        return impl.message;
    }

    /**
     * Sets the type of breadcrumb left - one of those enabled in
     * {@link Configuration#getEnabledBreadcrumbTypes()}
     *
     * @param type
     */
    public void setType(BreadcrumbType type) {
        if (type != null) {
            impl.type = type;
        } else {
            logNull("type");
        }
    }

    /**
     * Gets the type of breadcrumb left - one of those enabled in
     * {@link Configuration#getEnabledBreadcrumbTypes()}
     *
     * @return BreadcrumbType
     */

    public BreadcrumbType getType() {
        return impl.type;
    }

    /**
     * Sets diagnostic data relating to the breadcrumb
     *
     * @param metadata
     */
    public void setMetadata(Map<String, Object> metadata) {
        impl.metadata = metadata;
    }

    /**
     * Gets diagnostic data relating to the breadcrumb
     *
     * @return meta data
     */

    public Map<String, Object> getMetadata() {
        return impl.metadata;
    }

    /**
     * The timestamp that the breadcrumb was left
     *
     * @return time stamp
     */

    public Date getTimestamp() {
        return impl.timestamp;
    }

    String getStringTimestamp() {
        return DateUtils.toIso8601(impl.timestamp);
    }

    @Override
    public void toStream(JsonStream stream) throws IOException {
        impl.toStream(stream);
    }
}
